<?php

/**
 * RabbitCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@rabbitcms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Connection.php 3986 2010-07-25 16:32:46Z huuphuoc $
 * @since		2.0.0
 */
class Rabbit_Db_Adapter {
    const KEY = 'Rabbit_Db_Connection_Key';
    const PREFIX_KEY = 'Rabbit_Db_Connection_TablePrefix';

    /**
     * Default table prefix
     *
     * @var const
     * @since 2.0.3
     */
    const DEFAULT_PREFIX = 'rb_';

    protected $_adapter;

    /**
     * @return Rabbit_Db_Connection_Abstract
     */
    public static function factory() {
        $config = Rabbit_Config::getConfig();
        $adapter = $config->db->adapter;
        $adapter = str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));
        $instance = new self($adapter);
        return $instance;
    }

    public function __construct($adapter) {
        $this->_adapter = $adapter;
    }

    /**
     * @return string
     */
    public function getAdapter() {
        return $this->_adapter;
    }

    /**
     * Get database table prefix
     *
     * @since 2.0.3
     * @return string
     */
    public static function getDbPrefix() {
        if (!Zend_Registry::isRegistered(self::PREFIX_KEY)) {
            $config = Rabbit_Config::getConfig();
            /**
             * Note that I use === operator that allows user to use empty prefix
             */
            $prefix = (null === $config->db->prefix) ? self::DEFAULT_PREFIX : $config->db->prefix;
            Zend_Registry::set(self::PREFIX_KEY, $prefix);
        }
        return Zend_Registry::get(self::PREFIX_KEY);
    }

    /**
     * Abstract connection
     * @param array $config Database connection settings, includes parameters:
     * - host
     * - port
     * - dbname
     * - username
     * - password
     * - charset
     * @return mixed Database connection
     */
    protected function _connect($dbConfig) {
        try {
            $db = Zend_Db::factory('Pdo_Mysql', $dbConfig);
            $db->setFetchMode(Zend_Db::FETCH_OBJ);
            $db->query("SET CHARACTER SET 'utf8'");
            return $db;
        } catch (Exception $ex) {
            echo $ex->getMessage();
            return null;
        }
    }
    /**
     * @param string $type Type of connection. Must be slave or master
     * @return mixed
     */
    public function getConnection() {
        if (!Zend_Registry::isRegistered(self::KEY)) {
            $config = Rabbit_Config::getConfig();
            $server = $config->db;
            $server = $server->toArray();
            $prefix = (null == $config->db->prefix) ? self::DEFAULT_PREFIX : $config->db->prefix;
            $server['prefix'] = $prefix;
            $db = $this->_connect($server);
            Zend_Db_Table::setDefaultAdapter($db);
            Zend_Registry::set(self::KEY, $db);
        }
        return Zend_Registry::get(self::KEY);
    }

    

    public function getVersion() {
        $conn = $this->getConnection();
        $row = $conn->query('SELECT VERSION() AS ver')->fetch();
        return 'MySQL v' . $row->ver;
    }

    public function query($sql) {
        $conn = $this->getConnection();
        $conn->query($sql);
    }

}
